﻿Imports QuanLyLuong.DL
Imports QuanLyLuong.Entity

Public Class BLBase

    Protected dsBusiness As DataSet
    Protected m_PrimaryKey As String
    Protected m_TableName As String
    Protected drCurrence As DataRow
    Protected guidCurrentID As Guid
    Protected oDLBase As DL.DLBase
    Protected dtBusiness As DataTable

    Public Overridable Property BusinessObject() As DataSet
        Get
            Return dsBusiness
        End Get
        Set(ByVal value As DataSet)
            dsBusiness = value
        End Set
    End Property

    Public Overridable Property CurrenceBusiness() As DataRow
        Get
            Return drCurrence
        End Get
        Set(ByVal value As DataRow)
            drCurrence = value
        End Set
    End Property

    Public Overridable Property PrimaryKey() As String
        Get
            Return m_PrimaryKey
        End Get
        Set(ByVal value As String)
            m_PrimaryKey = value
        End Set
    End Property

    Public Overridable Property TableName() As String
        Get
            Return m_TableName
        End Get
        Set(ByVal value As String)
            m_TableName = value
            If oDLBase IsNot Nothing Then
                oDLBase.TableName = value
            End If

            If dsBusiness IsNot Nothing AndAlso dsBusiness.Tables.Contains(value) Then
                If dsBusiness.Tables(value).PrimaryKey.Length > 0 Then
                    Me.PrimaryKey = dsBusiness.Tables(value).PrimaryKey(0).ColumnName
                End If
                dtBusiness = dsBusiness.Tables(value)
            End If
        End Set
    End Property

    Public Overridable ReadOnly Property TableBusinessObject() As DataTable
        Get
            Return dtBusiness
        End Get
    End Property

    Public Sub New()
        InitPublicObject()
    End Sub

    Public Overridable Sub InitPublicObject()
        oDLBase = New DLBase(BLCommonFactory.DatabaseConnectName)
    End Sub

    ''' <summary>
    ''' Get All Dictionary Data
    ''' </summary>
    Public Overridable Sub GetAllData()
        Try
            If dsBusiness IsNot Nothing AndAlso dsBusiness.Tables.Contains(Me.TableName) Then
                dsBusiness.Tables(Me.TableName).Clear()
                oDLBase.GetAllData(dsBusiness.Tables(Me.TableName))
            End If
        Catch ex As Exception
            Throw
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="arrOption"></param>
    ''' <remarks></remarks>
    Public Overridable Sub GetAllDataFollowOption(ByVal arrOption As Object())
        If dsBusiness IsNot Nothing AndAlso dsBusiness.Tables.Contains(Me.TableName) Then
            dsBusiness.Tables(Me.TableName).Clear()
            oDLBase.GetAllDataFollowedOption(dsBusiness.Tables(Me.TableName), arrOption)
        End If
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="arrOption"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overridable Function InitNewRow(Optional ByVal arrOption As Object() = Nothing) As DataRow
        Return Nothing
    End Function

    Public Overridable Function InsertRow(ByVal row As DataRow) As Boolean
        Dim bResult As Boolean
        Try
            bResult = oDLBase.Insert(row)
        Catch ex As Exception
            Throw
        End Try
        Return bResult
    End Function

    Public Overridable Function UpdateMasterDetail(ByVal rowMaster As DataRow, ByVal dtDetail As DataTable, ByVal isAdd As Boolean) As Boolean

        Dim blnResult As Boolean = False
        Using cnn As Common.DbConnection = DLBase.Connection
            Dim ts As Common.DbTransaction
            cnn.Open()
            ts = cnn.BeginTransaction
            Try
                'Update Master Table
                If isAdd Then
                    If Me.SetNewRefNo(rowMaster, ts) Then
                        blnResult = oDLBase.Insert(rowMaster, ts)
                    End If
                Else
                    blnResult = oDLBase.Update(rowMaster, ts)
                End If

                If blnResult Then
                    If dtDetail Is Nothing OrElse oDLBase.UpdateData(dtDetail, ts) Then
                        ts.Commit()
                        blnResult = True
                    Else
                        ts.Rollback()
                        blnResult = False
                    End If
                Else
                    ts.Rollback()
                    blnResult = False
                End If

            Catch ex As Exception
                ts.Rollback()
                Throw
            Finally
                cnn.Close()
                cnn.Dispose()
            End Try
        End Using
        Return blnResult
    End Function


    Public Overridable Function UpdateRow(ByVal row As DataRow) As Boolean
        Dim bResult As Boolean
        Try
            bResult = oDLBase.Update(row)
        Catch ex As Exception
            Throw
        End Try
        Return bResult
    End Function

    Public Overridable Function DeleteRow(ByVal guiID As Guid) As Boolean
        Dim bResult As Boolean
        Try
            '----------------------------------------------
            'Kiểm tra Dictionary Row tồn tại hay không
            'Dim iCheckDictRowExistResult As Integer = oDictionaryService.CheckDictRowExist(guiID, m_strPrimaryKey)
            'Select Case iCheckDictRowExistResult
            '    Case -1 'Row không tồn tại
            '        Throw New ConcurrencyException(mscSQLExceptionConcurrence, iCheckDictRowExistResult)
            '    Case 0 'Row tồn tại

            'End Select
            '----------------------------------------------
            bResult = oDLBase.Delete(guiID)
            Return bResult
        Catch ex As Exception
            Throw
        End Try
    End Function

    Public Overridable Function DeleteRow(ByVal strID As String) As Boolean
        Dim bResult As Boolean
        Try
            '----------------------------------------------
            'Kiểm tra Dictionary Row tồn tại hay không
            'Dim iCheckDictRowExistResult As Integer = oDictionaryService.CheckDictRowExist(guiID, m_strPrimaryKey)
            'Select Case iCheckDictRowExistResult
            '    Case -1 'Row không tồn tại
            '        Throw New ConcurrencyException(mscSQLExceptionConcurrence, iCheckDictRowExistResult)
            '    Case 0 'Row tồn tại

            'End Select
            '----------------------------------------------
            bResult = oDLBase.Delete(strID)
            Return bResult
        Catch ex As Exception
            Throw
        End Try
    End Function

    Public Overridable Function UpdateTable(ByVal dtData As DataTable) As Boolean
        Dim bResult As Boolean
        Try
            If Not dtData Is Nothing Then
                bResult = oDLBase.UpdateData(dtData)
            Else
                bResult = True
            End If

        Catch ex As Exception
            Throw
        End Try
        Return bResult
    End Function

    ''' <summary>
    ''' Chap nhan viec Them, Sua, Xoa
    ''' </summary>
    ''' <remarks></remarks>
    Public Overridable Sub AcceptChange()
        If dsBusiness IsNot Nothing AndAlso dsBusiness.Tables.Contains(Me.TableName) Then
            dsBusiness.Tables(Me.TableName).AcceptChanges()
        End If
    End Sub

    ''' <summary>
    ''' Huy bo viec Them, Sua, Xoa. Dung khi Cancel
    ''' </summary>
    ''' <remarks></remarks>
    Public Overridable Sub RejectChange()
        If dsBusiness IsNot Nothing AndAlso dsBusiness.Tables.Contains(Me.TableName) Then
            dsBusiness.Tables(Me.TableName).RejectChanges()
        End If

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overridable Function CheckExistAlias(ByVal aliasFieldName As String, ByVal tableName As String, ByVal aliasValue As String, ByVal primaryKeyField As String, ByVal currrenID As Guid) As Boolean
        Return False
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overridable Function CheckExistAlias(ByVal aliasFieldName As String, ByVal aliasValue As String, ByVal currrenID As String) As Boolean
        Return oDLBase.CheckForDuplicatedAlias(currrenID, aliasValue, aliasFieldName, Me.PrimaryKey, Me.TableName)
    End Function

    'Public Overridable Function GetNewRefNo(ByVal eRefType As RefType) As String
    '    Dim strRefNo As String = ""
    '    Try
    '        strRefNo = oDLBase.GetNewRefNo(eRefType)
    '    Catch ex As Exception
    '        Throw
    '    End Try

    '    Return strRefNo
    'End Function

    Public Overridable Function SetNewRefNo(ByVal drMaster As DataRow, ByVal ts As Common.DbTransaction) As Boolean
        Try
            Return True
        Catch ex As Exception
            Throw
        End Try
    End Function
End Class
